package easybooking.db;

import easybooking.data.*;
import java.rmi.RemoteException;
import java.sql.*;
import java.util.Vector;

// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
// #[regen=yes,id=DCE.AC617AB5-8DCD-E232-1A7E-2789C75EEF56]
// </editor-fold>
public class AirportMapper {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.7137B4C7-A17E-1C03-E027-D5F4CB98D981]
    // </editor-fold>
    private static AirportMapper airportMapper = null;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.B18D8D89-E420-1B7B-EF6E-F6A683846310]
    // </editor-fold>
    public static AirportMapper airportMapper() {
        if (airportMapper == null) {
            airportMapper = new AirportMapper();
        }
        return airportMapper;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.E258E955-CA75-87F2-D3C6-3407F5C2AE55]
    // </editor-fold>
    public Airport findByKey(int id) {
        Connection con = DBConnection.connection();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT airportId, name, initials, city, isActive FROM Airport " +
                "WHERE airportId=" + id +
                " ORDER BY airportId ASC");
            if (rs.next()) {
                try {
                    Airport a = new AirportImpl();
                    a.setAirportId(rs.getInt("airportId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setCity(rs.getString("city"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    return a;
                } catch (Exception ex) {
                    ex.printStackTrace();
                    return null;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
            return null;
        }
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.FA5FAD00-5304-57EF-5B91-1468A3DB1708]
    // </editor-fold>
    public Vector<Airport> findAll() {
        Connection con = DBConnection.connection();
        Vector<Airport> result = new Vector<Airport>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT airportId, name, initials, city, isActive FROM Airport " +
                "ORDER BY airportId ASC");
            while (rs.next()) {
                try {
                    Airport a = new AirportImpl();
                    a.setAirportId(rs.getInt("airportId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setCity(rs.getString("city"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.891D813B-49E6-3A4C-A8F4-F520D4085066]
    // </editor-fold>
    public Airport insert(Airport a) {
        Connection con = DBConnection.connection();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT MAX(airportId) AS maxAirportId " +
                "FROM Airport");
            if (rs.next()) {
                try {
                    a.setAirportId(rs.getInt("maxAirportId") + 1);
                    stmt = con.createStatement();
                    stmt.executeUpdate("INSERT INTO Airport (airportId, name, initials, city, isActive) " +
                            "VALUES (" +
                            a.getAirportId() + ",'" +
                            a.getName() + "','" +
                            a.getInitials() + "','" +
                            a.getCity() + "','" +
                            a.getIsActive() + "')");
                }
                catch (RemoteException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return a;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.232C3D57-F75C-4993-A4B6-92D15EE2F916]
    // </editor-fold>
    public Airport update(Airport a) {
        Connection con = DBConnection.connection();
        try {
            Statement stmt = con.createStatement();
            stmt.executeUpdate("UPDATE Airport " +
            "SET name=\"" + a.getName() + "\", " +
            "initials=\"" + a.getInitials() + "\", " +
            "city=\"" + a.getCity() + "\", " +
            "isActive=\"" + a.getIsActive() + "\" " +
            "WHERE airportId=" + a.getAirportId());
        } catch (SQLException e2) {
            e2.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return a;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.39A65CE7-047A-1E88-308F-844882219EE6]
    // </editor-fold>
    public Vector<Airport> findByName(String name) {
        Connection con = DBConnection.connection();
        Vector<Airport> result = new Vector<Airport>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT airportId, name, initials, city, isActive FROM Airport " +
                "WHERE name LIKE " + name +
                "ORDER BY name ASC");
            while (rs.next()) {
                try {
                    Airport a = new AirportImpl();
                    a.setAirportId(rs.getInt("airportId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setCity(rs.getString("city"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.8064720D-6A3F-3D37-3075-5FC677AEADBD]
    // </editor-fold>
    public Vector<Airport> findByInitials(String initials) {
        Connection con = DBConnection.connection();
        Vector<Airport> result = new Vector<Airport>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT airportId, name, initials, city, isActive FROM Airport " +
                "WHERE inititals LIKE " + initials +
                "ORDER BY initials ASC");
            while (rs.next()) {
                try {
                    Airport a = new AirportImpl();
                    a.setAirportId(rs.getInt("airportId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setCity(rs.getString("city"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.CCC42977-C907-685E-AFDF-E8BE0D8A5111]
    // </editor-fold>
    public Vector<Airport> findByCity(String city) {
        Connection con = DBConnection.connection();
        Vector<Airport> result = new Vector<Airport>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT airportId, name, initials, city, isActive FROM Airport " +
                "WHERE city LIKE " + city +
                "ORDER BY city ASC");
            while (rs.next()) {
                try {
                    Airport a = new AirportImpl();
                    a.setAirportId(rs.getInt("airportId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setCity(rs.getString("city"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }
}

